package com.ushine.web.security.redis;

import com.ushine.web.component.contant.RedisConstant;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @Author: yls
 * @Date 2022/2/14 17:45
 * @Description: 登录的token信息
 * @Version 1.0
 **/
@Component
public class RedisTokenClient {

    @Resource
    private RedissonClient redissonClient;


    /**
     * 功能描述:
     * 〈获取用户缓存通道〉
     *
     * @param username 用户名
     * @return : org.redisson.api.RBucket<java.lang.String>
     * @author : yls
     * @date : 2022/2/14 17:55
     */
    public RBucket<String> getUserBucket(String username) {
        // 组装key
        String userKey = RedisConstant.TOKEN_PREFIX + RedisConstant.LOCK_SEPARATOR + username;
        // 获取用户的信息
        return redissonClient.getBucket(userKey);
    }

    /**
     * 功能描述:
     * 〈 获取用户在缓存中的token信息 〉
     *
     * @param username 用户名
     * @return : java.lang.String
     * @author : yls
     * @date : 2022/2/14 17:56
     */
    public String getUserToken(String username) {
        RBucket<String> userBucket = getUserBucket(username);
        String redisToken = userBucket.get();
        return Optional.ofNullable(redisToken).orElse("");
    }


}
